package com.infindo.iflower;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

import com.infindo.common.Constant;
import com.infindo.iflower.util.FileUtil;

public class GridFavorite extends Activity {
	private Button galleryBtn;
	private Button favoriteBtn;
	private Button aboutusBtn;
	private GridView g;
	private String[] picNames;
	private Activity mActivity;
	private static final int FAV_INTENT_CODE = 100;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.grid_gallery);
		mActivity = this;
		g = (GridView) findViewById(R.id.myGrid);
		initGallery();

		g.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long rowid) {
				Intent intent = new Intent(mActivity, PictureView.class);
				Bundle b = new Bundle();
				b.putInt("picIndex", Integer.parseInt(view.getTag()
								.toString()));
				b.putString(Constant.ClASS_METHOD, Constant.FILENAME_FROM_FAVORITE);
				b.putStringArray("picNames", picNames); // filenames from sd
				intent.putExtras(b);
				startActivityForResult(intent, FAV_INTENT_CODE);

			}
		});

		galleryBtn = (Button) findViewById(R.id.gallerybtn);
		galleryBtn.setOnTouchListener(touchLisenter);
		favoriteBtn = (Button) findViewById(R.id.favoritebtn);
		favoriteBtn.setOnTouchListener(touchLisenter);
		aboutusBtn = (Button) findViewById(R.id.aboutus);
		aboutusBtn.setOnTouchListener(touchLisenter);
		
		galleryBtn.setOnClickListener(new OnClickListener() {
			//			
			@Override
			public void onClick(View v) {
				Intent t = new Intent(mActivity,GridGallery.class);
				System.out.println("...........current time1.."+ System.currentTimeMillis());
				startActivity(t);
				finish();
			}
		});
		
		aboutusBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mActivity, AboutUs.class);
				startActivity(intent);
				finish();
			}
		});

		favoriteBtn.setBackgroundColor(Color.GRAY);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == FAV_INTENT_CODE){
			 if (resultCode == RESULT_OK) {
				 initGallery();
			 }
		}
	}
	private void initGallery() {
//		File file[] = android.os.Environment.getExternalStorageDirectory()
//				.listFiles();
		File file = new File(Constant.FAV_PATH);
		picNames = FileUtil.getsdfilenames(file);
		if (picNames == null)
			picNames = new String[0];
		g.setAdapter(Constant.getImageAdapterInstanceFavorite(this, this, picNames,Constant.FILENAME_FROM_FAVORITE));
//		g.invalidateViews();
	}

	private OnTouchListener touchLisenter = new OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {

			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				v.setBackgroundColor(Color.parseColor("#BDBDBD"));
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				// v.setBackgroundResource(R.drawable.play);
				v.setBackgroundColor(Color.parseColor("#848484"));
			}

			return false;
		}

	};

}
